package com.athuiju.fenxiaoback.controller.front;


import com.athuiju.fenxiaoback.entity.TbBanner;
import com.athuiju.fenxiaoback.entity.TbCategory;
import com.athuiju.fenxiaoback.entity.TbSpu;
import com.athuiju.fenxiaoback.entity.vo.FrontGoodVo;
import com.athuiju.fenxiaoback.entity.vo.IndexDataVo;
import com.athuiju.fenxiaoback.entity.vo.IndexSpuVo;
import com.athuiju.fenxiaoback.service.TbBannerService;
import com.athuiju.fenxiaoback.service.TbCategoryService;
import com.athuiju.fenxiaoback.service.TbSpuService;
import com.athuiju.fenxiaoback.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(description="nt首页",tags = "nt首页")
@RestController
@RequestMapping("/fenxiaoback01/indexfront")
public class IndexFrontController {

    @Autowired
    TbBannerService bannerService;

    @Autowired
    TbCategoryService categoryService;

    @Autowired
    TbSpuService spuService;

    @ApiOperation(value = "获取首页(轮播图，分类)数据")
    @GetMapping("getIndexFrontInfo")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = IndexDataVo.class)
    })
    public R getIndexFrontInfo() {

        IndexDataVo indexDataVo=new IndexDataVo();

        List<TbBanner> bannerlist = bannerService.list(null);
        indexDataVo.setBannerlist(bannerlist);


        List<TbCategory> categoryList = categoryService.list(new QueryWrapper<TbCategory>().eq("parent_id", 0));
        indexDataVo.setCategoryList(categoryList);


        //Page<TbSpu> pageSpu=new Page<>(1,10);
        //spuService.page(pageSpu,null);
        //
        //long total = pageSpu.getTotal();
        //List<TbSpu> goodList = pageSpu.getRecords();
        //
        //
        //ArrayList<IndexSpuVo> indexSpuVos=new ArrayList<>();
        //for(int i=0;i<goodList.size();i++){
        //    IndexSpuVo indexSpuVo=new IndexSpuVo();
        //    indexSpuVo.setSpuId(goodList.get(i).getId());
        //    indexSpuVo.setSpuTitle(goodList.get(i).getTitle());
        //
        //    indexSpuVos.add(indexSpuVo);
        //}
        //indexDataVo.setIndexSpuVos(indexSpuVos);

        return R.ok().put("data",indexDataVo);
    }




    @ApiOperation(value = "获取首页数据-商品分页查询")
    @GetMapping("getIndexFrontInfopage/{current}/{limit}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = IndexDataVo.class)
    })
    public R getIndexFrontInfopage(@PathVariable("current")  Long current,@PathVariable("limit") Long limit) {

        IndexDataVo indexDataVo=new IndexDataVo();

        //List<TbBanner> bannerlist = bannerService.list(null);
        //indexDataVo.setBannerlist(bannerlist);
        //
        //
        //List<TbCategory> categoryList = categoryService.list(new QueryWrapper<TbCategory>().eq("parent_id", 0));
        //indexDataVo.setCategoryList(categoryList);


        Page<TbSpu> pageSpu=new Page<>(current,limit);
        spuService.page(pageSpu,null);

        long total = pageSpu.getTotal();
        List<TbSpu> goodList = pageSpu.getRecords();



        ArrayList<IndexSpuVo> indexSpuVos=new ArrayList<>();
        for(int i=0;i<goodList.size();i++){
            IndexSpuVo indexSpuVo=new IndexSpuVo();
            indexSpuVo.setSpuId(goodList.get(i).getId());
            indexSpuVo.setSpuTitle(goodList.get(i).getTitle());
            //indexSpuVo.setDefaultSelectskuId(goodList.get(i).getDefaultSelectskuId());

            indexSpuVos.add(indexSpuVo);
        }
        indexDataVo.setIndexSpuVos(indexSpuVos);

        return R.ok().put("data",indexDataVo);
    }





}
